﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Smartec.Accounts.Camel.AMS.Model.Entities;
using Smartec.Accounts.Camel.AMS.Model.Repository;
using Smartec.Accounts.Camel.AMS.Model.Facades;

namespace Smartec.Accounts.Camel.AMS.WebApp.Controllers
{
    [Authorize]
    public class AdminTopicController : Controller
    {
        //
        // GET: /Topic/

        public ActionResult Index()
        {
            List<Topic> topics = TopicFacade.GetAllTopics();
            return View(topics);
        }

        //
        // GET: /Topic/Details/5

        public ActionResult Details(int id)
        {
            Topic topic = TopicFacade.GetTopicById(id);
            return View(topic);
        }

        //
        // GET: /Topic/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Topic/Create

        [HttpPost]
        public ActionResult Create(Topic topic)
        {
            try
            {
                // TODO: Add insert logic here
                TopicFacade.AddTopic(topic);
                TopicFacade.Save();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Topic/Edit/5
 
        public ActionResult Edit(int id)
        {
            Topic topic = TopicFacade.GetTopicById(id);
            return View(topic);
        }

        //
        // POST: /Topic/Edit/5
       
        [HttpPost]
        public ActionResult Edit(int id,Topic topic)
        {
            try
            {
                // TODO: Add update logic here
                TopicFacade.UpdateTopic(topic);
                TopicFacade.Save();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Topic/Delete/5
 
        public ActionResult Delete(int id)
        {
            Topic topic = TopicFacade.GetTopicById(id);
            return View(topic);
        }

        //
        // POST: /Topic/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                TopicFacade.DeleteTopicById(id);
                TopicFacade.Save();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
